﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Controls;
using ChoDiaOc_HK_WPF.Models;

namespace ChoDiaOc_HK_WPF.Views.TiepNhanDangKy {
    /// <summary>
    /// Interaction logic for DangKyKhachHang.xaml
    /// </summary>
    public partial class DangKyKhachHang : UserControl {
        public DangKyKhachHang() {
            InitializeComponent();
            khachHangSource = (KHACHHANG)this.FindResource("khachHangSource");
            TiepNhanDangKyController.GetInstance().KhachHangSource = khachHangSource;
            ResetSource();
        }

        private KHACHHANG khachHangSource;

        public KHACHHANG KhachHangSource {
            get { return khachHangSource; }
            set { khachHangSource = value; }
        }

        private List<String> listMaKH = null;

        public List<String> ListMaKH {
            get {
                if (listMaKH == null) {
                    listMaKH = DatabaseWrapper.GetInstance().GetAllMaKhachHang();
                }
                return listMaKH;
            }
            set { listMaKH = value; }
        }

        private bool isValidated = false;

        public bool IsValidated {
            get { return isValidated; }
            set { isValidated = value; }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
        }

        public void ResetSource() {
            khachHangSource = (KHACHHANG)this.FindResource("khachHangSource");
            khachHangSource.DiaChi = "";
            khachHangSource.HoTen = "";
            khachHangSource.MaKH = "";
            khachHangSource.SoDT = "";
            khachHangSource.Email = "";
            tenTextBox.Text = hoTextBox.Text = chuLotTextBox.Text = "";
            chkKhachHangCu.IsChecked = false;
            coboMaKhachHangCu.IsEnabled = false;
            btnChinhSua.IsEnabled = false;
            btnHoanTat.IsEnabled = true;
            btnNhapLai.IsEnabled = true;
        }

        public void Validate() {
            // Gather info into khachHangSource
            if (String.IsNullOrWhiteSpace(hoTextBox.Text) || String.IsNullOrWhiteSpace(tenTextBox.Text) || String.IsNullOrWhiteSpace(chuLotTextBox.Text)) {
                isValidated = false;
                return;
            }

            if (String.IsNullOrWhiteSpace(maKHTextBox.Text)) {
                isValidated = false;
                return;
            }

            if (String.IsNullOrWhiteSpace(diaChiTextBox.Text) || String.IsNullOrWhiteSpace(soDTTextBox.Text) || String.IsNullOrWhiteSpace(emailTextBox.Text)) {
                isValidated = false;
                return;
            }

            isValidated = true;
            // Validate khachHangSource
            // if invalidated -> MessageBox
            // if not, Go To next step & lock up => button to unlock + go back step
        }

        private void btnNhapLai_Click(object sender, RoutedEventArgs e) {

            ResetSource();
        }

        private void chkKhachHangCu_Checked(object sender, RoutedEventArgs e) {
            tenTextBox.IsReadOnly = true;
            hoTextBox.IsReadOnly = true;
            chuLotTextBox.IsReadOnly = true;
            diaChiTextBox.IsReadOnly = true;
            soDTTextBox.IsReadOnly = true;
            emailTextBox.IsReadOnly = true;
            coboMaKhachHangCu.IsEnabled = true;
            coboMaKhachHangCu.ItemsSource = ListMaKH;
            btnSinhMaKH.IsEnabled = false;
            TiepNhanDangKyController.GetInstance().IsKhachHangCu = true;
        }

        private void chkKhachHangCu_Unchecked(object sender, RoutedEventArgs e) {
            tenTextBox.IsReadOnly = false;
            hoTextBox.IsReadOnly = false;
            chuLotTextBox.IsReadOnly = false;
            diaChiTextBox.IsReadOnly = false;
            soDTTextBox.IsReadOnly = false;
            emailTextBox.IsReadOnly = false;
            coboMaKhachHangCu.IsEnabled = false;
            btnSinhMaKH.IsEnabled = true;
            TiepNhanDangKyController.GetInstance().IsKhachHangCu = false;
        }

        private void btnSinhMaKH_Click(object sender, RoutedEventArgs e) {
            khachHangSource.MaKH = TiepNhanDangKyController.GetInstance().GenerateMaKH();
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            Validate();
            if (isValidated) {
                khachHangSource.HoTen = hoTextBox.Text.Trim() + " " + chuLotTextBox.Text.Trim() + " " + tenTextBox.Text.Trim();
                App.ManHinhTiepNhanDangKy.DoNextStep(1);
                // Lock up
                tenTextBox.IsReadOnly = true;
                hoTextBox.IsReadOnly = true;
                chuLotTextBox.IsReadOnly = true;
                diaChiTextBox.IsReadOnly = true;
                soDTTextBox.IsReadOnly = true;
                emailTextBox.IsReadOnly = true;
                coboMaKhachHangCu.IsEnabled = false;
                btnSinhMaKH.IsEnabled = false;
                btnHoanTat.IsEnabled = false;
                btnChinhSua.IsEnabled = true;
                btnNhapLai.IsEnabled = false;
                chkKhachHangCu.IsEnabled = false;
                MessageBox.Show("Thông tin hợp lệ!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.None);
                //MessageBox.Show(khachHangSource.MaKH + khachHangSource.HoTen + khachHangSource.SoDT + khachHangSource.DiaChi + khachHangSource.Email);

                if (chkKhachHangCu.IsChecked == false) {
                    TiepNhanDangKyController.GetInstance().DangKyTaiKhoanKH();
                }
            } else {
                MessageBox.Show("Thông tin không hợp lệ!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.None);
            }
        }

        private void btnChinhSua_Click(object sender, RoutedEventArgs e) {
            // Unlock
            tenTextBox.IsReadOnly = false;
            hoTextBox.IsReadOnly = false;
            chuLotTextBox.IsReadOnly = false;
            diaChiTextBox.IsReadOnly = false;
            soDTTextBox.IsReadOnly = false;
            emailTextBox.IsReadOnly = false;
            coboMaKhachHangCu.IsEnabled = true;
            btnSinhMaKH.IsEnabled = true;
            btnHoanTat.IsEnabled = true;
            btnNhapLai.IsEnabled = true;
            chkKhachHangCu.IsEnabled = true;
            App.ManHinhTiepNhanDangKy.Step = 0;
            App.ManHinhTiepNhanDangKy.DoNextStep(0);
            App.ManHinhTiepNhanDangKy.DangKyDiaOcControl.btnChinhSua_Click(null, null);
        }

        private void coboMaKhachHangCu_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            String selected = (sender as ComboBox).SelectedItem as String;
            khachHangSource = DatabaseWrapper.GetInstance().GetKhachHang(selected);

            maKHTextBox.Text = khachHangSource.MaKH;
            diaChiTextBox.Text = khachHangSource.DiaChi;
            soDTTextBox.Text = khachHangSource.SoDT;
            emailTextBox.Text = khachHangSource.Email;

            String[] splits = khachHangSource.HoTen.Split(new char[] { ' ' });
            hoTextBox.Text = splits[0];
            tenTextBox.Text = splits[splits.Length - 1];
            chuLotTextBox.Text = "";
            for (int i = 1; i < splits.Length - 1; i++) {
                chuLotTextBox.Text += splits[i] + " ";
            }
        }
    }
}
